package changgou.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
    //令牌头名字
    private static final String AUTHORIZE_TOKEN = "Authorization";

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //用户如果是登录或者- -些不需要做权限认证的请求，直接放行
        String uri = request.getURI().getPath();
        if (URLFilter.hasAuthorize(uri)) {
            return chain.filter(exchange);
        }
//        //获取请求的URI
//        String path = request.getURI().getPath();
//
//        //如果是登录、goods等开放的微服务[这里的goods部分开放],则直接放行,这里不做完整演示，完整演示需要设计一套权限系统
//        if (path.startsWith("/user/login") || path.startsWith("/brand/search/")) {
//            //放行
//            Mono<Void> filter = chain.filter(exchange);
//            return filter;
//        }
        //获取ip MD5加密,再加入令牌
        //boolean
        //true:令牌在头文件中false: 令牌不在头 文件中->将令牌封装到头文件中，再传递给其他微服务
        boolean hasToken = true;
        //获取用户令牌信息
        //头文件
        String token = request.getHeaders().getFirst(AUTHORIZE_TOKEN);

        //第一个参数
        if (StringUtils.isEmpty(token)) {
            token = request.getQueryParams().getFirst(AUTHORIZE_TOKEN);
            hasToken = false;
        }
        if (StringUtils.isEmpty(token)) {
            HttpCookie httpCookie = request.getCookies().getFirst(AUTHORIZE_TOKEN);
            if (httpCookie != null) {
                token = httpCookie.getValue();
            }
        }

        //如果没有令牌，则拦截
        if (StringUtils.isEmpty(token)) {
            //设置没有权限的状态吗401
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //响应空数据
            return response.setComplete();
        }
//        try {
//            //解析令牌
////            JwtUtil.parseJWT(weixin);
//        } catch (Exception e) {
//            //无效则拦截
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
        //令牌为空，则允许访问，直接拦截bearer
        if (StringUtils.isEmpty(token)) {
            //设置没有权限的状态吗401
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //响应空数据
            return response.setComplete();
        } else {
            if (!hasToken) {
                //判断当前令牌是否有bearer前綴，如果没有，则添加前缀
                if (!token.startsWith("bearer ") && !token.startsWith("Bearer ")) {
                    token = "bearer " + token;
                }
            }
            //将令牌封装到头文件中.
            request.mutate().header(AUTHORIZE_TOKEN, token);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
